local skel = fk.CreateSkill {
  name = "emo__zhanwang",
}

Fk:loadTranslationTable{
  ["emo__zhanwang"] = "斩妄",
  [":emo__zhanwang"] = "出牌阶段各限一次，你可以令本回合所有角色禁止排序手牌，然后将最左/右侧的半数（向下取整，至少一张）手牌当【杀】使用，此牌只能被同侧的手牌响应。",

  ["#emo__zhanwang"] = "斩妄：将%arg %arg2张牌当【杀】使用",
  ["emo__zhanwang_left"] = "左侧",
  ["emo__zhanwang_right"] = "右侧",
  ["#emo__zhanwang-use"] = "斩妄：选择【杀】目标！",
}

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = function (self, player)
    local num = math.max(1, player:getHandcardNum() // 2)
    local choice = self.interaction.data
    if choice then
      return "#emo__zhanwang:::" .. choice .. ":" .. num
    end
    return " "
  end,
  target_num = 0,
  card_num = 0,
  interaction = function(self, player)
    local all_choices = {"emo__zhanwang_left", "emo__zhanwang_right"}
    local choices = table.filter(all_choices, function (choice)
      return player:getMark(choice .. "-phase") == 0
    end)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  can_use = function (self, player)
    return (player:getMark("emo__zhanwang_left-phase") == 0 or player:getMark("emo__zhanwang_right-phase") == 0)
    and not player:isKongcheng()
  end,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    local choice = self.interaction.data
    if not choice then return end
    room:setPlayerMark(player, choice .. "-phase", 1)
    room:syncPlayerClientCards(player) -- 先获取一下当前的手牌，防止用错
    local hand = player:getCardIds("h")
    local num = math.max(1, #hand // 2)
    if num > #hand then return end
    local cards = {}
    if choice:endsWith("left") then
      cards = table.slice(hand, 1, num + 1)
    else
      cards = table.slice(hand, #hand - num + 1, #hand + 1)
    end
    local use = room:askToUseVirtualCard(player, {
      name = "slash", skill_name = skel.name, subcards = cards, skip = true, cancelable = false,
      prompt = "#emo__zhanwang-use"
    })
    if not use then return end
    use.extra_data = use.extra_data or {}
    use.extra_data[skel.name] = choice
    for _, p in ipairs(room.players) do -- 成功使用了才禁止排序哈
      room:banSortingHandcards(p, "-" .. skel.name .."-turn")
    end
    room:useCard(use)
  end,
})

skel:addEffect(fk.HandleAskForPlayCard, {
  can_refresh = function(self, event, target, player, data)
    return data.eventData and data.eventData.from == player and (data.eventData.extra_data or {})[skel.name] ~= nil
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if not data.afterRequest then
      room:setBanner(skel.name, data.eventData.extra_data[skel.name])
    else
      room:setBanner(skel.name, 0)
    end
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if card.name == "jink" then
      local choice = Fk:currentRoom():getBanner(skel.name)
      if not choice then return false end
      local cards = Card:getIdList(card)
      local hand = player:getCardIds("h")
      if #cards == 0 or table.find(cards, function (id) -- 口嗨牌或非手牌区牌直接禁止
        return not table.contains(hand, id)
      end) then return true end
      if #cards > 1 then return true end
      if choice:endsWith("left") then
        return cards[1] ~= hand[1]
      else
        return cards[1] ~= hand[#hand]
      end
    end
  end,
})

return skel
